﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using OtherDevice;
using System.Diagnostics;
using CommonTools;
using System.Threading;
using BaseDll;
using UserData;
using System;
using MotionIoLib;
using Newtonsoft.Json;
namespace ModuleUnit
{
    public class Config_Plasma
    {
        [Category("工站参数"), DisplayName("1.Plasma设备 "), Description("Plasma设备 ")]
        public string str_Plasma { set; get; } = "";
        [Category("工站参数"), DisplayName("1.PlasmaIO "), Description("PlasmaIO ")]
        public string str_PlasmaIO { set; get; } = "";
        [Category("工站参数"), DisplayName("21.视觉控制"), Description("视觉控制")]
        public string str_VisionControl { set; get; } = "";
        [Category("工站参数"), DisplayName("相机编号"), Description("相机编号")]
        public int n_CameraID { set; get; } = 0;
        [Category("工站参数"), DisplayName("任务号/名称"), Description("任务号/名称")]
        public string str_任务号 { set; get; } = "";
    }
    [Description("Plasma")]

    public class Module_Plasma : ModuleBase
    {
        public Config_Plasma config = null;
        public Module_Plasma(object config, string strName) : base(strName)
        {
            if(config==null)
            {
                this.config = new Config_Plasma();
            }
            else
            {
                this.config = JsonConvert.DeserializeObject<Config_Plasma>(config.ToString());

            }
            this.obj_Config = this.config;
        }
        Plasma_Base Device_Plasma
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取串口(config.str_Plasma) as Plasma_Base;
            }
        }
        Vision_Base Device_视觉
        {
            set { }
            get
            {
                return DeviceMgr.GetInstance().Function_获取网口(config.str_VisionControl) as OtherDevice.Vision_Base;
            }
        }
        public bool Function_OpenPlasma(int n_Plasma功率, bool b_Plasma, int n_DelayTime, bool b_CheckPlasma,
                                         ref int n_ActPlasma功率, ref double d_Plasma气压, ref double d_AreaValue)
        {
            //若已打开火焰   直接跳过
            if (config.str_PlasmaIO != "" && config.str_PlasmaIO.GetOutput())
                return true;

            if (b_Plasma)
            {
                if (config.str_Plasma != "")
                {
                    Device_Plasma?.Function_SetPlasma功率(n_Plasma功率);
                    Thread.Sleep(300);
                    Device_Plasma?.OpenPlasma();
                }
                if (config.str_PlasmaIO != "")
                    config.str_PlasmaIO.SetIO(true);
                Thread.Sleep(n_DelayTime);
                if (config.str_Plasma != "")
                {
                    Device_Plasma?.Fucntion_GetPlasma功率(ref n_ActPlasma功率);
                    Device_Plasma?.Function_GetPlasma气压(ref d_Plasma气压);
                }
                if (b_CheckPlasma)
                {
                    string strMsg = "";
                    if (config.n_CameraID <0)
                        strMsg = config.str_任务号;
                    else
                        strMsg = $"{config.n_CameraID},{config.str_任务号}";

                    if (Device_视觉.VisionControlUsing(strMsg, out string[] result) == 0)
                    {
                        d_AreaValue = Convert.ToDouble(result[0]);
                    }
                    else
                    {
                        d_AreaValue = 0;
                    }
                }
            }
            return true;
        }
        public void Function_ClosePlasma()
        {
            if (config.str_Plasma != "")
            {
                Device_Plasma?.ClosePlasma();
            }
            if (config.str_PlasmaIO != "")
                config.str_PlasmaIO.SetIO(false);
        }
        public bool Function_GoPath(bool b_Circle, int n_AxisX, int n_AxisY, double d_Rx, double d_Ry,
                                    double d_OutLength, double d_PlasmaVel, double d_DispRunAngle,ref string strError)
        {
            if (b_Circle)
            {
                bool brtnExc = true;
                //获取当前坐标
                double d_NowPosX = MotionMgr.GetInstace().GetAxisActPos(n_AxisX);
                double d_NowPosY = MotionMgr.GetInstace().GetAxisActPos(n_AxisY);
                double DispPosX = MotionMgr.GetInstace().GetAxisActPos(n_AxisX) - (d_Rx + d_Ry) / 2;
                double DispPosY = MotionMgr.GetInstace().GetAxisActPos(n_AxisY) + d_OutLength;

                MotionMgr.GetInstace().ClearBufMove("Plasma群组");
                MotionMgr.GetInstace().RestGpErr("Plasma群组");

                MotionMgr.GetInstace().AddAxisToGroup("Plasma群组", new int[] { n_AxisX, n_AxisY });
                //加速度可考虑开放出来
                brtnExc &= MotionMgr.GetInstace().SetBufMoveParam("Plasma群组", d_PlasmaVel, d_PlasmaVel / 10, 0.1, 0.1, ref strError);
                if (!brtnExc)
                {
                    return false;
                }

                int CircleCount = 1;
                if (d_DispRunAngle % 360 == 0)
                    d_DispRunAngle = d_DispRunAngle + 0.1;
                CircleCount = 1 + (int)(d_DispRunAngle) / 360;
                double d_EndX = DispPosX + (d_Rx + d_Ry) / 2 * Math.Cos(d_DispRunAngle * Math.PI / 180);
                double d_EndY = DispPosY + (d_Rx + d_Ry) / 2 * Math.Sin(d_DispRunAngle * Math.PI / 180);

                 MotionMgr.GetInstace().AddBufMove("Plasma群组", BufMotionType.buf_Line2dAbs, 0, 2, d_PlasmaVel, 0, new double[2] { d_NowPosX, d_NowPosY }, new double[2] { d_NowPosX, d_NowPosY + d_OutLength });
                 MotionMgr.GetInstace().AddBufMove("Plasma群组", BufMotionType.buf_Arc2dAbsCW, 0, 2, d_PlasmaVel, 0, new double[2] { DispPosX, DispPosY }, new double[2] { d_EndX, d_EndY }, CircleCount);
                 MotionMgr.GetInstace().AddBufMove("Plasma群组", BufMotionType.buf_Line2dAbs, 0, 2, d_PlasmaVel, 0, new double[2] { d_NowPosX, d_NowPosY }, new double[2] { d_EndX, d_EndY + d_OutLength });
                if (config.str_PlasmaIO != "")
                {
                    //屏蔽自动关闭火焰，后续在主控软件中选择是否关闭
                    //MotionMgr.GetInstace().AddBufIo("Plasma群组", config.str_PlasmaIO, false);
                }
                MotionMgr.GetInstace().BufTrans("Plasma群组", ref strError);
                if (!brtnExc)
                {
                    return false;
                }
                brtnExc &= MotionMgr.GetInstace().AddBufEnd("Plasma群组");
                if (!brtnExc)
                {
                    return brtnExc;
                }
                MotionMgr.GetInstace().BufStart("Plasma群组");
                MotionMgr.GetInstace().ClearBufMove("Plasma群组");

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
                while (true)
                {
                    if (MotionMgr.GetInstace().IsAxisNormalStop(n_AxisX) == AxisState.NormalStop && MotionMgr.GetInstace().IsAxisNormalStop(n_AxisY) == AxisState.NormalStop)
                    {
                        break;
                    }
                    Thread.Sleep(5);
                    if (stopwatch.ElapsedMilliseconds > 30000)
                        return false;
                }
                return brtnExc;
            }
            else
            {
                bool brtnExc = true;
                //获取当前坐标
                double PlasmaPosX = MotionMgr.GetInstace().GetAxisActPos(n_AxisX);
                double PlasmaPosY = MotionMgr.GetInstace().GetAxisActPos(n_AxisY);
                MotionMgr.GetInstace().ClearBufMove("Plasma群组");
                MotionMgr.GetInstace().RestGpErr("Plasma群组");
                MotionMgr.GetInstace().AddAxisToGroup("Plasma群组", new int[] { n_AxisX, n_AxisY });
                //Plasma加速度可考虑开放出来
                brtnExc &= MotionMgr.GetInstace().SetBufMoveParam("Plasma群组", d_PlasmaVel, 0, 0.1, 0.1,ref strError);
                if (!brtnExc)
                {
                    return false;
                }
                XYUPoint[] point = new XYUPoint[7];
                point[0].x = PlasmaPosX;
                point[0].y = PlasmaPosY;
                point[1].x = PlasmaPosX;
                point[1].y = PlasmaPosY + d_OutLength;
                point[2].x = PlasmaPosX;
                point[2].y = PlasmaPosY + d_OutLength + d_Ry;
                point[3].x = PlasmaPosX - d_Rx;
                point[3].y = PlasmaPosY + d_OutLength + d_Ry;
                point[4].x = PlasmaPosX - d_Rx; ;
                point[4].y = PlasmaPosY + d_OutLength;
                point[5].x = PlasmaPosX;
                point[5].y = PlasmaPosY + d_OutLength;
                point[6].x = PlasmaPosX;
                point[6].y = PlasmaPosY;
                for (int i = 1; i < point.Length; i++)
                {
                     MotionMgr.GetInstace().AddBufMove("Plasma群组", BufMotionType.buf_Line2dAbs, 0, 2, d_PlasmaVel, 0, new double[] { point[i - 1].x, point[i - 1].y }, new double[] { point[i].x, point[i].y });
                }
                if (config.str_PlasmaIO != "")
                {
                    //屏蔽自动关闭火焰，后续在主控软件中选择是否关闭
                    //MotionMgr.GetInstace().AddBufIo("Plasma群组", config.str_PlasmaIO, false);
                }
                brtnExc &= MotionMgr.GetInstace().BufTrans("Plasma群组",ref strError);
                if (!brtnExc)
                {
                    return false;
                }
                MotionMgr.GetInstace().AddBufEnd("Plasma群组");
                if (!brtnExc)
                {
                    return brtnExc;
                }
                MotionMgr.GetInstace().BufStart("Plasma群组");
                MotionMgr.GetInstace().ClearBufMove("Plasma群组");

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
                while (true)
                {
                    if (MotionMgr.GetInstace().IsAxisNormalStop(n_AxisX) == AxisState.NormalStop && MotionMgr.GetInstace().IsAxisNormalStop(n_AxisY) == AxisState.NormalStop)
                    {
                        break;
                    }
                    Thread.Sleep(5);
                    if (stopwatch.ElapsedMilliseconds > 30000)
                        return false;
                }

                return brtnExc;

            }
        }


    }
}